/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.website.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.dao.gen.bean.BizWeb;
import com.nmf.dao.gen.BizWebMapper;

import com.nmf.site.biz.website.service.BizWebService;

/**
 * 网站信息Controller
 * @author bm
 * @version 2019-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/website/bizWeb")
public class BizWebController extends BaseController {

	@Autowired
	private BizWebService bizWebService;
	
	@ModelAttribute
	public BizWeb get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizWeb entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizWebService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizWeb();
		}
		return entity;
	}
	
	@RequiresPermissions("website:bizWeb:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizWeb bizWeb, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizWeb> page = bizWebService.findPage(new Page<BizWeb>(request, response), bizWeb); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/website/bizWebList";
		}
		return "biz/website/bizWebList";
	}
	
	@RequiresPermissions("website:bizWeb:view")
	@RequestMapping(value = "form")
	public String form(BizWeb bizWeb, Model model) {
		model.addAttribute("bizWeb", bizWeb);
		return "biz/website/bizWebForm";
	}

	@RequiresPermissions("website:bizWeb:edit")
	@RequestMapping(value = "save")
	public String save(BizWeb bizWeb, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizWeb)){
			return form(bizWeb, model);
		}
		try{
			bizWebService.save(bizWeb);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/website/bizWeb/?repage";
		}
		addMessage(redirectAttributes, "保存网站成功");
		return "redirect:"+Global.getAdminPath()+"/website/bizWeb/?repage";
	}
	
	@RequiresPermissions("website:bizWeb:edit")
	@RequestMapping(value = "delete")
	public String delete(BizWeb bizWeb, RedirectAttributes redirectAttributes) {
		try{
			bizWebService.delete(bizWeb);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/website/bizWeb/?repage";
		}
		addMessage(redirectAttributes, "删除网站成功");
		return "redirect:"+Global.getAdminPath()+"/website/bizWeb/?repage";
	}

}